
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shareName1: '',
        shareName2: '',
        myUsers: [],
        codeImg: '',
        path: app.globalData.path,
        user: '',
        url: 'https://zbz-imgs.oss-cn-beijing.aliyuncs.com/mini/',
        parent1: 0,
        parent2: 0,
    },


    // 解绑
    unbindHandle(e) {
        var user = e.currentTarget.dataset.obj, flag = e.currentTarget.dataset.flag, url = app.globalData.url + '/user/unbindUser', that = this;
        console.log(user);
        wx.showModal({
            title: '提示',
            content: `是否与${user.userName}解除绑定关系`,
            cancelText: '取消',
            confirmText: '解绑',
            complete: (res) => {
                if (res.confirm) {
                    wx.showLoading({
                        title: '',
                    })
                    app.wxRequest(url, {
                        userId: flag == 0 ? app.globalData.userId : user.userId,
                    }, res => {
                        console.log(res);
                        if (res.flag) {
                            that.getData()
                            wx.hideLoading()
                            wx.stopPullDownRefresh()
                        }
                    },
                        err => {
                            console.log(err);
                        })
                }
            }
        })
    },

    // 下载图片
    downloadImg() {
        let that = this;
        if (that.data.codeImg == '') {
            wx.showToast({
                title: '推广码暂未生成',
                icon: 'none'
            })
            return
        }
        wx.showLoading({
            title: '保存中...'
        });
        wx.downloadFile({
            url: that.data.codeImg,
            success: function (res) {
                wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: function (data) {
                        wx.hideLoading();
                        wx.showToast({
                            title: '保存成功',
                        })

                    },
                    fail: function (err) {
                        if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err
                            .errMsg === "saveImageToPhotosAlbum:fail auth deny" || err
                                .errMsg === "saveImageToPhotosAlbum:fail authorize no response"
                        ) {
                            wx.showModal({
                                title: '提示',
                                content: '需要您授权保存相册',
                                modalType: false,
                                success: modalSuccess => {
                                    wx.openSetting({
                                        success(settingdata) {
                                            console.log("settingdata",
                                                settingdata)
                                            if (settingdata
                                                .authSetting[
                                                'scope.writePhotosAlbum'
                                            ]) {
                                                wx.showModal({
                                                    title: '提示',
                                                    content: '获取权限成功,再次点击图片即可保存',
                                                    modalType: false,
                                                })
                                            } else {
                                                wx.showModal({
                                                    title: '提示',
                                                    content: '获取权限失败，将无法保存到相册哦~',
                                                    modalType: false,
                                                })
                                            }
                                        },
                                        fail(failData) {
                                            console.log("failData",
                                                failData)
                                        },
                                        complete(finishData) {
                                            console.log("finishData",
                                                finishData)
                                        }
                                    })
                                }
                            })
                        }
                    },
                    complete(res) {
                        wx.hideLoading(); //隐藏 loading 提示框
                    }
                })
            }
        })
    },

    // 获取推广码
    getCodeImg() {
        var that = this;
        wx.showLoading({
            title: '',
        })
        var url = app.globalData.url + "user/cenerateTheQrCode",
            that = this;
        app.wxRequest(url, {
            userId: app.globalData.userId,
        }, res => {
            console.log(res);
            if (res == 'success') {
                that.setData({
                    codeImg: `${that.data.url}${app.globalData.userId}.png`
                })

                wx.setStorageSync('userCodeImg', that.data.codeImg)

                wx.hideLoading()
                wx.stopPullDownRefresh()
            }

        },
            err => {
                console.log(err);
            })
    },


    getHigherUser() {

        wx.showLoading({
            title: '',
        })
        var url = app.globalData.url + "user/listHigherUser",
            that = this;
        app.wxRequest(url, {
            userId: app.globalData.userId,
        }, res => {
            console.log(res);
            if (res.flag) {
                console.log(res.data);
                that.setData({
                    shareName1: res.data.grandsire != null ? res.data.grandsire.userName || '用户' : '',
                    parent1: res.data.grandsire != null ? res.data.grandsire : '',
                    shareName2: res.data.pater != null ? res.data.pater.userName || '用户' : '',
                    parent2: res.data.pater != null ? res.data.pater : '',
                })
                console.log(that.data);
                wx.hideLoading()
                wx.stopPullDownRefresh()
            }
        },
            err => {
                console.log(err);
            })
    },


    getData() {
        wx.showLoading({
            title: '',
        })
        var url = app.globalData.url + "user/listLowerUser",
            that = this, myUsers = [];
        app.wxRequest(url, {
            userId: app.globalData.userId,
        }, res => {
            console.log(res);
            if (res.flag) {
                var datas = res.data;
                console.log(datas);
                myUsers.push(...datas.xj)
                myUsers.push(...datas.xxj)
                that.setData({
                    myUsers
                })
                console.log(that.data);
                wx.hideLoading()
                wx.stopPullDownRefresh()
            }
        },
            err => {
                console.log(err);
            })
    },



    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (wx.getStorageSync('userCodeImg')) {
            this.setData({
                codeImg: wx.getStorageSync('userCodeImg')
            })
        } else {
            this.getCodeImg()
        }
        this.getHigherUser()
        this.getData()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.getHigherUser()
        this.getData()
        this.getCodeImg()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})